﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotShot.Entities;

namespace HotShot.Service.BusinessLogic.MessageProcessors
{
    public class StoreResultProcessor : MessageProcessorBase  
    {
        public override void ProcessMessage(HotShot.Entities.Messages.BaseMessage message, string correlationId)
        {
            var entity = message.XmlPayload.FromXml<StorageEntity>();
            var result = MessageCorrelationLogic.Current.AddCorrelatedMessage(correlationId, message.SenderAddress, message);

            if (result == null) 
                return;

            if(result.ResponseReady(MessagingLogic.Current.StoreThreshold)) 
            {
                entity.StoreCount = result.ResponseTotal;
                entity.StoreIndex = result.ResponseCount;
                QueueCommunicationLogic.Current.SendOutputMessage(Messages.StoreResult(entity), correlationId);

                MessageCorrelationLogic.Current.FinalizeCorrelation(correlationId);
            }
        }
    }
}
